package com.itjeffrey.autocode.common;

import com.itjeffrey.autocode.util.BeanOprUtils;
import lombok.Data;

import java.util.List;

/**
 * 分页VO
 * 注意：分页VO在实例化时需要设置当前页和每页条数，可以调用setCurPageAndSize()实现
 *
 * @From: Jeffrey
 * @Date: 2020/11/9
 */
@Data
public class PageResult<T> {

    //总条数
    private Integer totals;

    //总页数
    private Integer pages;

    //当前页
    private Integer curPage;

    //每页条数
    private Integer size;

    //分页数据
    private List<T> data;

    /**
     * 设置当前页和每页条数
     *
     * @param pageForm
     * @return
     */
    public PageResult<T> setCurPageAndSize(PageForm<?> pageForm) {
        if (pageForm.getCurPage() == null) {
            pageForm.setCurPage(1); //防止Null指针
        }
        if (pageForm.getSize() == null) {
            pageForm.setSize(5);
        }
        BeanOprUtils.copyProperties(pageForm, this);
        return this;
    }

    /**
     * 设置总条数
     *
     * @param totals
     */
    public void setTotals(Integer totals) {
        this.totals = totals;
        this.pages = this.totals % this.size > 0 ? (this.totals / this.size + 1) : (this.totals / this.size);
    }

}
